package gavdi.dk.controller;

import org.json.JSONObject;



import gavdi.dk.data.DAOAbsence;
import gavdi.dk.data.DAOCustomer;
import gavdi.dk.data.DAOEvent;
import gavdi.dk.model.User;
import gavdi.dk.util.HttpNodeClient;
import gavdi.dk.util.Tools;
import Exceptions.ConverterException;
import Exceptions.HttpNodeClientException;
import android.content.Context;

import android.os.Bundle;
import android.text.Editable;




public class LoginController  {
	
	
	

	public static void startLoginProcess(Editable s, Context context) {
		
		// Creates Test Data
		
		String userID = s.toString();
		
		// Sends a Request to the node.js. 
		// Returns a true of false, if contact to node.js was established
		// If contact was established, then the node.js will get some data from SAP.
		sendLoginRequest(userID);
		
		// If the boolean is true, then it will wait a 1-2 seconds.
		// Then contact the node.js again, and retrieve the updated data.
		// It will then update the SQLite database
		updateDBdata(context);

		// Regardless if contact to node.js was established and the database was updated.
		// This entire method WILL get the updated or non-updated from the mySQL database and return it as a bundle.
		
		//getLoginData(context, userID);

	
}


	public static void sendLoginRequest(String userID){
			
			JSONObject jsonLogin;
		    try {
		    	HttpNodeClient.startConnection();
		    	
				jsonLogin = Tools.createLoginJSON(userID);
				HttpNodeClient.sendJSON(jsonLogin);
				 
			} catch (ConverterException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (HttpNodeClientException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			 
		
	}
	
	public static void updateDBdata(Context context) {
		// opdater database, med data fra node.js
		
	}
	
	public static Bundle getLoginData(Context context, String userID){

    	
    	
		// Get data from DB and return arraylist
		User user;
		DAOEvent dEvent = new DAOEvent(context);
		DAOCustomer dCustomer = new DAOCustomer(context);
		DAOAbsence dAbsence = new DAOAbsence(context);
	
		Bundle bundle = new Bundle();
		long start = System.currentTimeMillis();
		user = Controller.loadUserData(context, userID);
		long total = (System.currentTimeMillis()-start);
		System.out.println("Det tog: " + total + " millisekunder at hente User data");
		
		
		start = System.currentTimeMillis();
		
		
		bundle.putSerializable("user", user);
		bundle.putSerializable("allEvents", dEvent.getAllEvents());
		bundle.putSerializable("allWBS", dCustomer.getAllCustomers());
		bundle.putSerializable("allAbsence", dAbsence.getAllAbsence());
		total = (System.currentTimeMillis()-start);
		System.out.println("Det tog : " + total + " millisekunder at putte data i bundle");
		
		
		return bundle;
	}
	
	public static boolean requestUpdate(Context context, String id) {
		// TODO Auto-generated method stub
		return false;
	}


	
}
